package com.boc.ewr.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.SignMismatchFocusList;
import com.boc.ewr.bcimp.mapper.SignMismatchFocusListMapper;
import com.boc.ewr.risk.entity.SignMismatchFdbk;
import com.boc.ewr.risk.entity.SignPluralMismatch;
import com.boc.ewr.risk.mapper.SignPluralMismatchMapper;
import com.boc.ewr.risk.service.ISignPluralMismatchService;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysDepartModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description: 多次“验印不符”清单
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class SignPluralMismatchServiceImpl extends ServiceImpl<SignPluralMismatchMapper, SignPluralMismatch> implements ISignPluralMismatchService {

    @Autowired
    private SignMismatchFocusListMapper signMismatchFocusListMapper;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    /**
     * 同协议号下账单需要多次验印通过/未通过记录
     * 导入不符重点关注清单后，形成多次“验印不符”清单，要求二级行或网点提醒客户印章是否需要更换或改正盖章习惯。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        // 清除当前数据
        // this.remove(null);

        //生成数据
        LambdaQueryWrapper<SignMismatchFocusList> wrapper = new LambdaQueryWrapper<>();
        // todo 如何定义 多次“验印不符”（此处通过不符回执次数） 次数待定
        wrapper.ge(SignMismatchFocusList::getRtnErrNum, 1);
        List<SignMismatchFocusList> signMismatchFocusLists = signMismatchFocusListMapper.selectList(wrapper);
        if (signMismatchFocusLists.size() == 0) {
            return false;
        }

        List<SignPluralMismatch> signPluralMismatchList = new ArrayList<>();
        // 将推送的签约开户行
        for (SignMismatchFocusList signMismatchFocusList : signMismatchFocusLists) {
            SignPluralMismatch signPluralMismatch = new SignPluralMismatch();
            BeanUtils.copyProperties(signMismatchFocusList, signPluralMismatch);

            // 重置Id
            signPluralMismatch.setId(null);
            String orgDeptId = sysBaseAPI.getDepartIdsByOrgCode(signMismatchFocusList.getOrgBranchNo());
            SysDepartModel sysDepartModel = sysBaseAPI.selectAllById(orgDeptId);
            // 签约行/开户行名称
            signPluralMismatch.setOrgBranchNm(sysDepartModel.getDepartName());
            // 管辖行(编号)
            SysDepartModel parentDeptModel = sysBaseAPI.selectAllById(sysDepartModel.getParentId());
            signPluralMismatch.setAdminOrgNo(parentDeptModel.getOrgCode());
            // 协议号
            signPluralMismatch.setCaProtocolNo(signMismatchFocusList.getProtocolNo());

            signPluralMismatchList.add(signPluralMismatch);
        }

        this.sendMsg(signPluralMismatchList);
        return this.saveOrUpdateBatch(signPluralMismatchList);
    }

    private void sendMsg(Collection<SignPluralMismatch> entityList) {

        Set<String> orgSet = new HashSet();
        for (SignPluralMismatch signPluralMismatch : entityList) {
            orgSet.add(signPluralMismatch.getOrgBranchNo());
        }

        // 信息推送至网点对账人员
        String toUser = ToUserUtil.genToUser(orgSet);
        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "多次“验印不符”",
                    "不符重点关注清单中存在多次“验印不符”数据，请您及时查看并完成排查，提醒客户印章是否需要更换或改正盖章习惯",
                    MsgCategoryEnum.RISK_CHECK.getIndex());
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }

    }
}
